package com.imooc.order.service.impl;

import com.imooc.order.ClientFeign.ProductClient;
import com.imooc.order.common.DecreaseStockInput;
import com.imooc.order.common.ProductInfoOutput;
import com.imooc.order.dataobject.OrderDetail;
import com.imooc.order.dataobject.OrderMaster;
import com.imooc.order.dataobject.ProductInfo;
import com.imooc.order.dto.OrderDTO;
import com.imooc.order.enums.OrderStatusEnum;
import com.imooc.order.enums.PayStatusEnum;
import com.imooc.order.repository.OrderDetailRepository;
import com.imooc.order.repository.OrderMasterRepository;
import com.imooc.order.service.OrderService;
import com.imooc.order.utils.KeyUtil;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.List;
import java.util.stream.Collectors;


@Service
public class OrderServiceImpl  implements OrderService{

    @Autowired
    private OrderMasterRepository orderMasterRepository ;

    @Autowired
    private OrderDetailRepository orderDetailRepository ;

    @Autowired
    ProductClient  productClient ;

    /**
     * 创建订单接口
     *
     * 1，查询商品信息
     * 2。计算中加
     * 3。扣库存
     * 4。订单入库
     * @param orderDTO
     * @return
     */
    @Override
    public OrderDTO create(OrderDTO orderDTO) {
        //1，查询商品信息
        List<String> productList = orderDTO.getOrderDetailList().stream()
                .map(OrderDetail::getProductId)
                .collect(Collectors.toList());

        List<ProductInfoOutput>  producrtInfoList  = productClient.listForOrder(productList);

        String orderId = KeyUtil.genUniqueKey();
        //2。计算总价
        BigDecimal orderAmount = BigDecimal.ZERO;
        for (OrderDetail  orderDetail : orderDTO.getOrderDetailList()){
            for (ProductInfoOutput productInfo: producrtInfoList){
                if(productInfo.getProductId().equals(orderDetail.getProductId())){
                    /**d单价*数量*/
                    orderAmount= productInfo.getProductPrice()
                            .multiply(new BigDecimal(orderDetail.getProductQuantity()))
                            .add(orderAmount);

                    BeanUtils.copyProperties(productInfo,orderDetail);

                    orderDetail.setOrderId(orderId);
                    orderDetail.setDetailId(KeyUtil.genUniqueKey());

                    //订单明细入库
                    orderDetailRepository.save(orderDetail);
                }

            }

        }

        //3.抵扣库存（调用商品服务）
        List<DecreaseStockInput> decreaseStockInputList = orderDTO.getOrderDetailList().stream()
                .map(e->new DecreaseStockInput(e.getProductId(),e.getProductQuantity()))
                .collect(Collectors.toList());

        productClient.descreaseStock(decreaseStockInputList);

        //4. 订单入库
        OrderMaster orderMaster = new OrderMaster();

        BeanUtils.copyProperties(orderDTO,orderMaster);
        orderDTO.setOrderId(orderId);
        BeanUtils.copyProperties(orderDTO, orderMaster);
        orderMaster.setOrderAmount(orderAmount);
        orderMaster.setOrderStatus(OrderStatusEnum.NEW.getCode());
        orderMaster.setPayStatus(PayStatusEnum.WAIT.getCode());
        orderMasterRepository.save(orderMaster);

        return orderDTO;





    }


    /**
     * 完结订单(只能卖家操作)
     * @param orderId
     * @return
     */
  public   OrderDTO finish(String orderId){
        return  new OrderDTO();
    }



}
